//
//  ScoreViewController.m
//  F&B BallScorer
//
//  Created by dev1 on 2019/6/13.
//  Copyright © 2019 dev1. All rights reserved.
//

#import "ScoreViewController.h"
#import "NSTimer+XZAdd.h"
@interface ScoreViewController ()
@property (nonatomic, weak) NSTimer *countdownTimer;

@property (weak, nonatomic) IBOutlet UIImageView *ballImageview;
@property (weak, nonatomic) IBOutlet UILabel *teamALbl;
@property (weak, nonatomic) IBOutlet UILabel *teamBLbl;
@property (weak, nonatomic) IBOutlet UILabel *durationLbl;
@property (weak, nonatomic) IBOutlet UILabel *AScoreLbl;
@property (weak, nonatomic) IBOutlet UILabel *BScoreLbl;
@property (weak, nonatomic) IBOutlet UIButton *AjianBtn;
@property (weak, nonatomic) IBOutlet UIButton *AjiaBtn;
@property (weak, nonatomic) IBOutlet UIButton *BjianBtn;
@property (weak, nonatomic) IBOutlet UIButton *BjiaBtn;
@property (weak, nonatomic) IBOutlet UIButton *EndBtn;

@end

@implementation ScoreViewController
{
    double _bTime;
    double _fTime;
}
- (void)addObserverFunc {
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hadEnterBackGround) name:UIApplicationWillResignActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hadEnterForeGround) name:UIApplicationDidBecomeActiveNotification object:nil];
}
- (void)hadEnterBackGround{
    _bTime = [self getTimestamp];
    [self stopTimer];
}
- (void)hadEnterForeGround{
    _fTime = [self getTimestamp];
    
    NSInteger cha = _fTime - _bTime;
    NSInteger shengyu = _items.duration;
    NSInteger nst = shengyu - cha;
    if (nst >= 0) {
        _items.duration = nst;
    } else {
        _items.duration = 0;
    }
    [self startCountdown];
}
- (double)getTimestamp{
    NSDate *lastDate = [NSDate date];
    return [self getTimestampWithDate:lastDate];
}
- (long)getTimestampWithDate:(NSDate *)date {
    double firstStamp = [date timeIntervalSince1970];
    return firstStamp;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addObserverFunc];
    
    if (!_items) {
        _items = [[teamItem alloc]init];
    }
    
    [_items addObserver:self
               forKeyPath:@"Ascore"
                  options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
                  context:nil];
    [_items addObserver:self
               forKeyPath:@"Bscore"
                  options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
                  context:nil];
    
    
    
    self.ballImageview.image = [UIImage imageNamed:_ball==kBasketball?@"篮球":@"足球"];
    self.teamALbl.text = _items.Aname;
    self.teamBLbl.text = _items.Bname;
    self.durationLbl.text = [NSString stringWithFormat:@"%02ld:%02ld:%02ld",_items.duration/60/60, _items.duration/60%60, _items.duration % 60];
    
    [self startCountdown];
}
- (void)startCountdown {
    [self stopTimer];
    self.countdownTimer = [NSTimer xz_scheduledTimerWithTimeInterval:1.f block:^(NSTimer * _Nonnull timer) {
        [self countdownStop];
    } repeats:YES];
}

- (void)countdownStop{
    NSInteger total = _items.duration;
    
    if (total == 0) {
        [self endScore];
    } else {
        total --;
        _items.duration = total;
    }
    
    self.durationLbl.text = [NSString stringWithFormat:@"%02ld:%02ld:%02ld",_items.duration/60/60, _items.duration/60%60, _items.duration % 60];
    
}
- (void)stopTimer {
    if (self.countdownTimer) {
        [self.countdownTimer invalidate];
        self.countdownTimer = nil;
    }
}
- (void)endScore {
    [self stopTimer];
    self.AjiaBtn.hidden = YES;
    self.AjianBtn.hidden = YES;
    self.BjianBtn.hidden = YES;
    self.BjiaBtn.hidden = YES;
    self.EndBtn.hidden = YES;
}
- (IBAction)didClickDismiss:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)didClickEnd:(UIButton *)sender {
    [self endScore];
}

- (IBAction)didClickAJian:(id)sender {
    NSInteger score = _items.Ascore;
    if (score > 0) {
        score --;
        _items.Ascore = score;
    }
}
- (IBAction)didClickAJia:(id)sender {
    _items.Ascore ++;
}
- (IBAction)didClickBJian:(id)sender {
    NSInteger score = _items.Bscore;
    if (score > 0) {
        score --;
        _items.Bscore = score;
    }
}
- (IBAction)didClickBJia:(id)sender {
    _items.Bscore ++;
}


#pragma mark -
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:@"Ascore"]) {
        _AScoreLbl.text = [NSString stringWithFormat:@"%ld", _items.Ascore];
    }
    if ([keyPath isEqualToString:@"Bscore"]) {
        _BScoreLbl.text = [NSString stringWithFormat:@"%ld", _items.Bscore];
    }
}

- (void)dealloc {
    [_items removeObserver:self forKeyPath:@"Ascore"];
    [_items removeObserver:self forKeyPath:@"Bscore"];
    [self stopTimer];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
